package com.ossjk.ossjkservlet.system.servlet;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ossjk.core.base.servlet.BaseServlet;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.AjaxReturn;
import com.ossjk.core.vo.Page;
import com.ossjk.ossjkservlet.common.Constant;
import com.ossjk.ossjkservlet.common.UUIDUtils;
import com.ossjk.ossjkservlet.system.dao.CustomerDao;

import com.ossjk.ossjkservlet.system.pojo.Customer;

@WebServlet("/system/customer.do")
public class CustomerServlet extends BaseServlet {

	private Logger logger = Logger.getLogger(CustomerServlet.class);
	private CustomerDao customerDao  = new CustomerDao();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		String method = getMethod(req);
		if ("list".equals(method)) {
			list(req, resp);
		} else if ("toInsert".equals(method)) {
			toInsert(req, resp);
		} else if ("insert".equals(method)) {
			insert(req, resp);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, resp);
		} else if ("update".equals(method)) {
			update(req, resp);
		} else if ("delete".equals(method)) {
			delete(req, resp);
		} else if ("batchDelete".equals(method)) {
			batchDelete(req, resp);
		}
	}

	/**
	 * 列表
	 * 
	 * @param req
	 * @param resp
	 */
	public void list(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Page page = getParamtoObject(Page.class, req);
			String sql = customerDao.getAllSql();
			sql += " where 1 = 1 ";
			List args = new ArrayList();
			if (!CommonUtil.isBlank(req.getParameter("name"))) {
				sql += "and name like  CONCAT('%',?,'%')";
				args.add(req.getParameter("name"));
				page.getPrams().put("name", req.getParameter("name"));
			}
			req.setAttribute("page", customerDao.selectByPageCount(sql, page, args.toArray()));
			// 转发
			forward(req, resp, "/WEB-INF/page/system/customer/list.jsp");
		} catch (Exception e) {
			logger.error("", e);
			// 重定向
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}

	}

	/**
	 * 去增加
	 * 
	 * @param req
	 * @param resp
	 */
	public void toInsert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			forward(req, resp, "/WEB-INF/page/system/customer/edit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去更新
	 * 
	 * @param req
	 * @param resp
	 */
	public void toUpdate(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.setAttribute("record", customerDao.selectById(req.getParameter("id")));
			forward(req, resp, "/WEB-INF/page/system/customer/edit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 增加
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void insert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			// 假设表单和实体类字段一致，可以调用getParamtoObject方法把表单内容自动封装到实体类当中
			Customer customer = getParamtoObject(Customer.class, req);
			// 把日期格式的数据设置到对象中
						String birthStr = req.getParameter("birth");
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						customer.setBirth(sdf.parse(birthStr));

						// id没有、字符串类型的id、uuid
						customer.setId(UUIDUtils.generateUUID());
			if (customerDao.insert(customer) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 更新
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Customer customer = getParamtoObject(Customer.class, req);
			
			if (customerDao.updateBySelected(customer) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void delete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (customerDao.delete(req.getParameter("id")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void batchDelete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (customerDao.batchDelete(req.getParameterValues("ids")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	public static void main(String[] args) {
		
	}

}
